package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShopCartMapper;
import com.sky.service.ShopCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShopCartServiceImpl implements ShopCartService {

    @Autowired
    private ShopCartMapper shopCartMapper;

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    @Override
    public void add(ShoppingCartDTO shoppingCartDTO) {
        // 构造查询对象
        ShoppingCart shoppingCart = new ShoppingCart();
        shoppingCart.setUserId(BaseContext.getCurrentId());
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);

        // 查询添加的菜品是否已经存在
        List<ShoppingCart> shoppingCartList = shopCartMapper.select(shoppingCart);

        // 添加菜品存在，更新菜品数量
        if(shoppingCartList != null && shoppingCartList.size() == 1) {
            ShoppingCart tmp = shoppingCartList.get(0);
            tmp.setNumber(tmp.getNumber() + 1);
            shopCartMapper.updateNumberById(tmp);
        }
        else {      // 添加菜品不存在，插入
            if(shoppingCart.getDishId() != null) {      // 插入的是菜品数据
                Dish dish = dishMapper.selectById(shoppingCart.getDishId());
                shoppingCart.setName(dish.getName());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setAmount(dish.getPrice());
            }
            else {
                Setmeal setmeal = setmealMapper.selectById(shoppingCart.getSetmealId());
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setAmount(setmeal.getPrice());
            }
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shopCartMapper.insert(shoppingCart);
        }

    }

    @Override
    public List<ShoppingCart> showShopCart() {
        return shopCartMapper.selectByUserId(BaseContext.getCurrentId());
    }

    @Override
    public void deleteOne(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId());
        List<ShoppingCart> shoppingCartList = shopCartMapper.select(shoppingCart);
        if(shoppingCartList != null && shoppingCartList.size() == 1) {
            ShoppingCart tmp = shoppingCartList.get(0);
            if(tmp.getNumber() == 1) {
                shopCartMapper.deleteOne(tmp);
            }
            else {
                tmp.setNumber(tmp.getNumber() - 1);
                shopCartMapper.updateNumberById(tmp);
            }
        }



    }

    @Override
    public void clean() {
        shopCartMapper.cleanByUserId(BaseContext.getCurrentId());
    }
}
